local zhuanzhen = fk.CreateSkill {
  name = "lvshi__zhuanzhen",
  tags = { Skill.Limited }
}

Fk:loadTranslationTable{
  ["lvshi__zhuanzhen"] = "转阵",
  [":lvshi__zhuanzhen"] = "限定技，一名角色的回合结束后，你可令其执行一个额外回合，此回合的出牌阶段开始时，你视为对其使用智策略【暗度陈仓】；此回合结束时，其选择弃置四张牌或翻面。",

  ["@@lvshi__zhuanzhen"] = "转阵",
  ["#lvshi__zhuanzhen-invoke"] = "转阵：你可以令%src执行一个额外的回合",

  ["$lvshi__zhuanzhen1"] = "将军百战余生，发可白而志不可改！",
  ["$lvshi__zhuanzhen2"] = "壮士沙场效死，命可折而膝不可届！",
}

zhuanzhen:addEffect(fk.EventPhaseStart, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(zhuanzhen.name) and target.phase == Player.Play
    and target:getMark("@@lvshi__zhuanzhen") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:useVirtualCard("lvshi__secret_advance", nil, player, target, zhuanzhen.name, true)
  end,
})

zhuanzhen:addEffect(fk.PreCardUse, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(zhuanzhen.name) and target == player and data.card.skillName == zhuanzhen.name
    and data.card.type == Card.TypeTrick
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addCardMark(data.card, "strategyscheme")
  end,
})

zhuanzhen:addEffect(fk.TurnEnd, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if data.reason == zhuanzhen.name and data.extra_data and data.extra_data.zhuanzhen_source then
      return data.extra_data.zhuanzhen_source == player and not target.dead
    else
      return player:hasSkill(zhuanzhen.name) and not target.dead and player:usedSkillTimes(zhuanzhen.name, Player.HistoryGame) == 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if data.reason == zhuanzhen.name and data.extra_data and data.extra_data.zhuanzhen_source then
      return true
    else
      return player.room:askToSkillInvoke(player, {skill_name = zhuanzhen.name, prompt = "#lvshi__zhuanzhen-invoke:"..target.id})
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.reason == zhuanzhen.name and data.extra_data and data.extra_data.zhuanzhen_source then
      local result = room:askToDiscard(target, {
        min_num = 4,
        max_num = 4,
        include_equip = true,
        skill_name = zhuanzhen.name,
        cancelable = true,
      })
      if #result == 0 then
        target:turnOver()
      end
    else
      room:notifySkillInvoked(player, zhuanzhen.name, "big")
      player:broadcastSkillInvoke(zhuanzhen.name)
      room:setPlayerMark(target, "@@lvshi__zhuanzhen", 1)
      target:gainAnExtraTurn(true, zhuanzhen.name, nil, {
        zhuanzhen_source = player
      })
    end
  end,

  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    return data.reason == zhuanzhen.name and data.extra_data and data.extra_data.zhuanzhen_source
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:setPlayerMark(target, "@@lvshi__zhuanzhen", 0)
  end,
})

return zhuanzhen
